﻿#include "CBlue.h"

CBlue::CBlue(float x, float y)
{
	m_TypeMove = TypeMove::Move_OnTheLand;

	if (m_TypeMove == TypeMove::Move_OnTheLand)
		m_IdImage = IDImage::IMG_STAND;
	else m_IdImage = IDImage::IMG_RUN_WATER_RIGHT;

	m_Flip = SpriteEffect::None;
	m_Sprite = Source::getInstance()->getSprite(m_IdImage);
	m_IdGun = IDGun::Plasma_Gun;

	m_Explode = new Explode();
	m_Explode->Init(IDImage::IMG_BlueExplode);

	m_TimeAnimation = 2.5 * ((float)1 / FPS);

	m_TimeJump = 0;
	m_ToolBullet = new Toolbullet(10, 1.0f);
	m_Vpx = 0;
	m_Gametime = new GameTime();

	m_Vpy = VIEW_PORT_Y;
	m_Position.y = y;
	m_Position.x = x;
	m_isAttack = false;

	loadPositionGunAtSprite(PositionGunAtSprite);
	m_Status = IDStatus::FALL_DOWN;
}

void CBlue::Update()
{
	// update effect explode
	UpdateAnimation();

	if (m_IsEffect)
	{
		if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation * 1.75)
		{
			m_Gametime->Update();

			m_Explode->Next();
			m_Position.x -= m_Sprite->getWidth() / 3;

			if (m_Explode->NoneEffect())
				m_Status = IDStatus::DIE;
		}

		return;
	}

	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation && m_Status != IDStatus::DIE && m_Status != IDStatus::BEFORE_DIE)
	{
		m_Gametime->Update();
		
		if (m_Status == IDStatus::JUMP_UP || m_Status == IDStatus::AFTER_JUMP) 
		{
			m_TimeJump -= m_TimeAnimation;
			m_Veclocity_vx *= 1.15f;
			m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_JUMP);
			m_Sprite->Next();

			if (m_TimeJump <= 0)
			{
				m_Status = IDStatus::AFTER_JUMP;
				m_Veclocity_vy = 0.0f;
				m_TimeJump = 0.0f;
			}
			else
				m_Position.y += m_Veclocity_vy;
		}
		else
		{
			m_Sprite = Source::getInstance()->getSprite(m_IdImage);
		}
		
		if (m_TimeShoot > 0)
		{
			m_TimeShoot -= m_TimeAnimation;
			m_Sprite->Next();
		}
		else 
		{
			if ( m_IdImage == IDImage::IMG_RIGHT_LEFT || m_IdImage == IDImage::IMG_45_DEGREE || m_IdImage == IDImage::IMG_315_DEGREE 
				|| m_IdImage == IDImage::IMG_RUN_WATER_RIGHT || m_IdImage == IDImage::IMG_RUN_WATER_DROP)
					m_Sprite->Next();
		}

		if (m_isAttack)
		{
			m_ToolBullet->setGun(m_IdGun);
			UpdateShoot();
		}


		m_Position.x += m_Veclocity_vx;
		if (m_Status != IDStatus::JUMP_UP)
			m_Position.y += m_Gravity;


		// update camera
		if (m_Veclocity_vx > 0)
		{
			if (m_Vpx <= m_Position.x - SCREEN_WIDTH / 2)
				m_Vpx = m_Position.x - SCREEN_WIDTH / 2;
		}
		if (m_Vpx <= 0) m_Vpx = 0;

		// Không cho hero đi ra khỏi camera
		if (m_Position.x <= m_Vpx) m_Position.x = m_Vpx;

		// update lai trong luc
		m_Gravity = -FALLDOWN_VELOCITY_DECREASE * 4 / 5;
	}
	
	m_ToolBullet->Update();
}

void CBlue::UpdateShoot()
{
	for (int i = 0; i < m_positionShootAtSprite.size(); i++)
	{
		if (m_TypeMove == TypeMove::Move_OnTheLand)// neu sprite la di chuyen tren bo
		{
			if (m_IdImage == IDImage::IMG_DOWN)// lay toa do sung khi nam xuong
			{
				// ID_POSITION_GUN_SPRITE_DOWN quy dinh id trong file text la sprite nam xuong
				if (m_shootDirection == m_positionShootAtSprite[i].m_Index && m_positionShootAtSprite[i].m_Id == ID_POSITION_GUN_SPRITE_DOWN)
				{
					// m_positionShootAtSprite[i].m_X / 2 vi ve~ tu tam cua sprite
					m_ToolBullet->InitBullet(m_Position.x, m_Position.y,m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, m_TypeMove);
					m_ToolBullet->Shoot();
					break;
				}
			}
			else // khi khong nam xuong
			{
				// ID_POSITION_GUN_SPRITE_DOWN quy dinh id trong file text cac loai action: right, stand, up, goc 45, goc 315
				if (m_positionShootAtSprite[i].m_Index == m_shootDirection && m_positionShootAtSprite[i].m_Id == ID_POSITION_GUN_ONTHELAND)
				{
					
					m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, m_TypeMove);
					m_ToolBullet->Shoot();
					break;
				}
			}

			if (m_Status == IDStatus::JUMP_UP || m_Status == IDStatus::AFTER_JUMP) // toa do sung khi nhay len se ve tu tam cua sprite
			{
				m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_Sprite->getWidth() / 2, 0, m_Flip, m_shootDirection, m_TypeMove);
				m_ToolBullet->Shoot();
				break;
			}
		}
		else // neu cac sprite dang di chuyen duoi nuoc
		{
			if (m_positionShootAtSprite[i].m_Index == m_shootDirection  && m_positionShootAtSprite[i].m_Id == ID_POSITION_GUN_UNDERWATER)
			{
				m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, m_TypeMove);
				m_ToolBullet->Shoot();
				break;
			}
		}	
	}

	m_isAttack = false;
}

void CBlue::UpdateAnimation()
{
	// dua vao cac huong khi an key ma quy dinh loai image se duoc load len
	if (m_TypeMove == TypeMove::Move_OnTheLand)
	{
		switch (m_shootDirection)
		{
		case _0_SHOOT:
			if (m_isAttack)
				m_IdImage = IDImage::IMG_RUN_SHOOT;
			else
				m_IdImage = IDImage::IMG_RIGHT_LEFT;
			break;
		case _45_SHOOT:
			m_IdImage = IDImage::IMG_45_DEGREE;
			break;
		case _90_SHOOT:
			m_IdImage = IDImage::IMG_UP;
			break;
		case _270_SHOOT:
			if ((m_Status == IDStatus::JUMP_UP || m_Status == IDStatus::AFTER_JUMP) && m_isAttack)
			{
				m_shootDirection = ShootDirection::_270_SHOOT;
			}
			else if (m_Status == IDStatus::FALL_DOWN)
			{
				m_IdImage = IDImage::IMG_STAND;
			}
			else
			{
				m_IdImage = IDImage::IMG_DOWN;
				m_shootDirection = ShootDirection::_0_SHOOT;
			}

			break;
		case _315_SHOOT:
			m_IdImage = IDImage::IMG_315_DEGREE;
			break;
		case NONE:// khong key nao duoc an
			m_IdImage = IDImage::IMG_STAND;
			m_shootDirection = ShootDirection::_0_SHOOT;
			break;
		}
	}
	else if (m_TypeMove == TypeMove::Move_UnderWater)
	{
		switch (m_shootDirection)
		{
		case _0_SHOOT:case NONE:
			m_shootDirection = ShootDirection::_0_SHOOT;

			if (m_isAttack)
				m_IdImage = IDImage::IMG_RUN_WATER_SHOOT_0;
			else
				m_IdImage = IDImage::IMG_RUN_WATER_RIGHT;

			break;
		case _45_SHOOT:
			m_IdImage = IDImage::IMG_RUN_WATER_45;
			break;
		case _90_SHOOT:
			m_IdImage = IDImage::IMG_RUN_WATER_UP;
			break;
		case _270_SHOOT:case _315_SHOOT:
			m_Veclocity_vx = 0;
			m_IdImage = IDImage::IMG_RUN_WATER_DROP;
			m_shootDirection = ShootDirection::NONE;
			m_isAttack = false;
			m_TimeShoot = 0;
			break;
		}
	}
}

void CBlue::InputKey(CKeyBoard keyboard)
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		keyboard.GetState();
		m_Veclocity_vx = SPEED;

		if (keyboard.IsKeyDown(DIK_RIGHT) && keyboard.IsKeyDown(DIK_UP))
		{
			m_Flip = SpriteEffect::None;
			m_shootDirection = ShootDirection::_45_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_RIGHT) && keyboard.IsKeyDown(DIK_DOWN))
		{
			m_Flip = SpriteEffect::None;
			m_shootDirection = ShootDirection::_315_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT) && keyboard.IsKeyDown(DIK_DOWN))
		{
			m_Veclocity_vx = -SPEED;
			m_Flip = SpriteEffect::Flip;
			m_shootDirection = ShootDirection::_315_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT) && keyboard.IsKeyDown(DIK_UP))
		{
			m_Veclocity_vx = -SPEED;
			m_Flip = SpriteEffect::Flip;
			m_shootDirection = ShootDirection::_45_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_LEFT))
		{
			m_Veclocity_vx = -SPEED;
			m_Flip = SpriteEffect::Flip;
			m_shootDirection = ShootDirection::_0_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_RIGHT))
		{
			m_Flip = SpriteEffect::None;
			m_shootDirection = ShootDirection::_0_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_UP))
		{
			m_Veclocity_vx = 0;
			m_shootDirection = ShootDirection::_90_SHOOT;
		}
		else if (keyboard.IsKeyDown(DIK_DOWN))
		{
			m_Veclocity_vx = 0;
			m_shootDirection = ShootDirection::_270_SHOOT;
		}
		else
		{
			m_Veclocity_vx = 0;
			m_shootDirection = ShootDirection::NONE;
		}


		if (keyboard.IsPressed(DIK_SPACE)  && m_Status == IDStatus::STAND && m_TypeMove == TypeMove::Move_OnTheLand)
		{
			if (m_shootDirection != ShootDirection::_270_SHOOT)
			{
				m_Status = IDStatus::JUMP_UP;
				m_TimeJump = 6 * m_TimeAnimation;
				m_Veclocity_vy = VECLOCITY_VY;
			}
			else
			{
				m_Status = IDStatus::FALL_DOWN;
				m_Position.y -= 12;
			}
		}


		// phim ban chinh
		if (keyboard.IsPressed(DIK_Z))
		{
			m_isAttack = true;
			m_TimeShoot = 2 * m_TimeAnimation;
			m_IdGun = IDGun::Spread_Gun;
		}

		if (keyboard.IsKeyDown(DIK_X))
		{
			m_isAttack = true;
			m_TimeShoot = 2 * m_TimeAnimation;
			m_IdGun = IDGun::Machine_Gun;
		}

		if (keyboard.IsPressed(DIK_C))
		{
			m_IsEffect = true;
			m_Status = IDStatus::BEFORE_DIE;
		}
	}	
}

void CBlue::UpdateCollisionMap(vector<ObjectTittle> &NodeTitle)
{
	m_Box = getBouding();

	// thiet lap vi tri tuong doi cua Box can xet va cham 

	m_Box.x += (float)m_Sprite->getWidth() / 4;
	m_Box.width -= (float)m_Sprite->getWidth() / 2;

	m_Box.vy += m_Gravity;// bi gia toc trong truong keo xuong
	m_Box.height = 3;// gia han chieu cao

	Character::UpdateCollisionMap(NodeTitle);
}

void CBlue::setTypeMove(TypeMove type)
{
	m_TypeMove = type;
}


CBlue::~CBlue()
{
}


